package GUI;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import Controllers.ClientController;

import Controllers.MainWindowController;
import Entities.LoginEntity;

/**
 * The Class MainWindowGUI.
 * On this window we put all the panels in the project .
 */
public class MainWindowGUI extends JFrame {

	/** The content pane. */
	private JPanel contentPane;
	private LoginEntity loginEnt;
	

	/**
	 * Create the frame.
	 */
	public MainWindowGUI() {
			
		initialize();
	}

	/**
	 * Instantiates a new main window gui.
	 *
	 * @param contentPane the content pane
	 */
	public MainWindowGUI(JPanel contentPane,LoginEntity loginEnt) {
		this.loginEnt=loginEnt;
		this.contentPane=contentPane;
		initialize();
	}

	/**
	 * Initialize.
	 */
	public void initialize() {
		setTitle("CCRM");
		setLayout(null);
		setSize(500,400);
		setLocationRelativeTo(null);
		setResizable(false);
		add(contentPane);
		setContentPane(contentPane);
	//	setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				ClientController.setObserver(new MainWindowController());
				String query="UPDATE users SET `loginStatus`='0' WHERE `uid`='"+loginEnt.getUid()+"'";			//updating login status query.
				try {
					ClientController.observableClient.sendToServer(query);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});


		setVisible(true);
	}
	
}
